home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Base / sa / char < prev    next >
Text File  |  1996-07-18  |  13KB  |  308 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  3. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  4. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  5. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  6. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  7. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  8.  
  9. -- char.sa: Characters.
  10. -------------------------------------------------------------------
  11. immutable class CHAR < $IS_LT{CHAR}, $HASH, $STR, $FMT is
  12.    -- Objects which represent characters.
  13.    -- This version is for ASCII. Other implementations might use
  14.    -- UNICODE, etc.
  15.    
  16.    -- AVAL isn't handled right yet, leave out for the moment.
  17.    -- include AVAL{BOOL} asize->;
  18.    include COMPARABLE;
  19.    
  20.    const asize:=SYS::char_size;
  21.  
  22.    is_alpha:BOOL is
  23.       -- True if self is an alphabetic character.
  24.       case self
  25.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  26.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  27.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  28.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  29.       then return true 
  30.       else return false end end;
  31.  
  32.    is_upper:BOOL is
  33.       -- True if self is uppercase.
  34.       case self
  35.       when 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  36.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 
  37.       then return true
  38.       else return false end end;
  39.    
  40.    is_lower:BOOL is
  41.       -- True if self is lowercase.
  42.       case self
  43.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  44.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' 
  45.       then return true
  46.       else return false end end;
  47.  
  48.    is_octal_digit:BOOL is
  49.       -- True if self is an octal digit.
  50.       case self
  51.       when '0', '1', '2', '3', '4', '5', '6', '7'
  52.       then return true
  53.       else return false end end;
  54.       
  55.    octal_digit_value:INT is
  56.       -- The numerical value of self as an octal digit. -1 if not.
  57.       case self 
  58.       when '0' then return 0   when '1' then return 1
  59.       when '2' then return 2   when '3' then return 3
  60.       when '4' then return 4   when '5' then return 5
  61.       when '6' then return 6   when '7' then return 7 
  62.       else return -1 end end;
  63.       
  64.    is_digit:BOOL is 
  65.       -- True if self is a digit.
  66.       case self
  67.       when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
  68.       then return true
  69.       else return false end end;
  70.  
  71.    digit_value:INT is
  72.       -- The numerical value of self as a decimal digit.
  73.       -- -1 if it isn't.
  74.       case self 
  75.       when '0' then return 0   when '1' then return 1
  76.       when '2' then return 2   when '3' then return 3
  77.       when '4' then return 4   when '5' then return 5
  78.       when '6' then return 6   when '7' then return 7
  79.       when '8' then return 8   when '9' then return 9 
  80.       else return -1 end end;
  81.    
  82.    is_hex_digit:BOOL is 
  83.       -- True if self is a hexadecimal digit.
  84.       case self
  85.       when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  86.      'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'
  87.       then return true
  88.       else return false end end;
  89.  
  90.    hex_digit_value:INT is
  91.       -- The numerical value of self as a hexadecimal digit.
  92.       -- -1 if it isn't.
  93.       case self 
  94.       when '0' then return 0   when '1' then return 1
  95.       when '2' then return 2   when '3' then return 3
  96.       when '4' then return 4   when '5' then return 5
  97.       when '6' then return 6   when '7' then return 7
  98.       when '8' then return 8   when '9' then return 9 
  99.       when 'a','A' then return 10  when 'b','B' then return 11 
  100.       when 'c','C' then return 12  when 'd','D' then return 13 
  101.       when 'e','E' then return 14  when 'f','F' then return 15 
  102.       else return -1 end end;
  103.    
  104.    is_alphanum:BOOL is
  105.       -- True if self is alphanumeric.
  106.       case self
  107.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
  108.      'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
  109.      'z',
  110.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  111.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  112.      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
  113.       then return true
  114.       else return false end end;
  115.    
  116.    is_space:BOOL is
  117.       -- True if self is one of the whitespace characters:
  118.       -- space, form feed, newline, carriage return, tab, vertical tab.
  119.       -- NOTE: temporarily Ctrl-Z will be treated as whitespace. This
  120.       --       ensures OS/2 compatibility until FILE classes are fixed.
  121.       case self
  122.       when ' ', '\f', '\n', '\r', '\t', '\v', '\032'
  123.       then return true
  124.       else return false end end;
  125.  
  126.    is_print:BOOL is
  127.       -- True if self is a printing character.
  128.       case self
  129.       when ' ', 
  130.      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  131.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  132.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  133.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  134.      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  135.      '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', 
  136.      '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', 
  137.      ']', '^', '_',    '`', '{', '|', '}', '~',','
  138.       then return true
  139.       else return false end end;
  140.    
  141.    is_punct:BOOL is 
  142.       -- True if self is punctuation.
  143.       case self
  144.       when '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', 
  145.      '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', 
  146.      ']', '^', '_',    '`', '{', '|', '}','~',','
  147.       then return true
  148.       else return false end end;     
  149.  
  150.    is_control:BOOL is
  151.       -- True if self is a control character.
  152.       return self.int<32;
  153.    end;
  154.  
  155.    int:INT is
  156.       -- The integer version of self.  Returns unsigned value.  Built-in.
  157.       builtin CHAR_INT; 
  158.       end;
  159.  
  160.    hash:INT is
  161.       -- A very cheap, but not very good hash value. It just returns 
  162.       -- the hash of the integer representation of the char.
  163.       return int.hash; end;
  164.       
  165.    ascii_int:INT is
  166.       -- The ASCII integer associated with each character in the
  167.       -- minimal Sather character set. Returns -1 for other characters.
  168.       case self
  169.       -- This case commented out due to a bug in gdb not recognizing \a
  170.       -- when '\a' then return 7
  171.       when '\b' then return 8      
  172.       when '\t' then return 9       when '\n' then return 10      
  173.       when '\v' then return 11      when '\r' then return 13
  174.       when ' ' then return 32       when '!' then return 33 
  175.       when '\"' then return 34      when '#' then return 35 
  176.       when '$' then return 36       when '%' then return 37 
  177.       when '&' then return 38       when '\'' then return 39
  178.       when '(' then return 40       when ')' then return 41
  179.       when '*' then return 42       when '+' then return 43
  180.       when ',' then return 44       when '-' then return 45
  181.       when '.' then return 46       when '/' then return 47
  182.       when '0' then return 48       when '1' then return 49
  183.       when '2' then return 50       when '3' then return 51
  184.       when '4' then return 52       when '5' then return 53
  185.       when '6' then return 54       when '7' then return 55
  186.       when '8' then return 56       when '9' then return 57
  187.       when ':' then return 58       when ';' then return 59
  188.       when '<' then return 60       when '=' then return 61
  189.       when '>' then return 62       when '?' then return 63
  190.       when '@' then return 64       when 'A' then return 65
  191.       when 'B' then return 66       when 'C' then return 67
  192.       when 'D' then return 68       when 'E' then return 69
  193.       when 'F' then return 70       when 'G' then return 71
  194.       when 'H' then return 72       when 'I' then return 73
  195.       when 'J' then return 74       when 'K' then return 75
  196.       when 'L' then return 76       when 'M' then return 77
  197.       when 'N' then return 78       when 'O' then return 79
  198.       when 'P' then return 80       when 'Q' then return 81
  199.       when 'R' then return 82       when 'S' then return 83
  200.       when 'T' then return 84       when 'U' then return 85
  201.       when 'V' then return 86       when 'W' then return 87
  202.       when 'X' then return 88       when 'Y' then return 89
  203.       when 'Z' then return 90       when '[' then return 91
  204.       when '\\' then return 92      when ']' then return 93
  205.       when '^' then return 94       when '_' then return 95
  206.       when '`' then return 96       when 'a' then return 97
  207.       when 'b' then return 98       when 'c' then return 99
  208.       when 'd' then return 100      when 'e' then return 101
  209.       when 'f' then return 102      when 'g' then return 103
  210.       when 'h' then return 104      when 'i' then return 105
  211.       when 'j' then return 106      when 'k' then return 107
  212.       when 'l' then return 108      when 'm' then return 109
  213.       when 'n' then return 110      when 'o' then return 111
  214.       when 'p' then return 112      when 'q' then return 113
  215.       when 'r' then return 114      when 's' then return 115
  216.       when 't' then return 116      when 'u' then return 117
  217.       when 'v' then return 118      when 'w' then return 119
  218.       when 'x' then return 120      when 'y' then return 121
  219.       when 'z' then return 122      when '{' then return 123
  220.       when '|' then return 124      when '}' then return 125
  221.       when '~' then return 126      
  222.       else return -1 end end; 
  223.  
  224.    from_ascii_int(i:INT):SAME 
  225.       -- The ASCII character corresponding to `i' for characters
  226.       -- in the Sather set, and the character with code `i' for
  227.       -- the remainder.
  228.       pre i.is_bet(0,127) is
  229.       return ("\0\1\2\3\4\5\6\a"  "\b\t\n\v\14\r\16\17"
  230.       "\20\21\22\23\24\25\26\27" "\30\31\32\33\34\35\36\37"
  231.       " !\"#$%&'"  "()*+,-./"  "01234567"  "89:;<=>?"  "@ABCDEFG"
  232.       "HIJKLMNO"  "PQRSTUVW"  "XYZ[\\]^_"  "`abcdefg"  "hijklmno"
  233.       "pqrstuvw"  "xyz{|}~\177")[i] end;      
  234.  
  235.    str:STR is
  236.       return #STR(self); end;
  237.  
  238.    fmt( f: STR ): STR
  239.    is
  240.       return BASE_FORMAT::fmt_char( self, f )
  241.    end;
  242.    
  243.    pretty:STR is
  244.       -- A pretty version of self. It includes single quotes
  245.       -- around self and uses special codes or the octal representation
  246.       -- for non printing characters.
  247.       if is_print and self/='\'' and self/='\\' then 
  248.      return "'" + self + "'" end; 
  249.       case self 
  250.       when '\a' then return "'\\a'"  when '\b' then return "'\\b'"     
  251.       when '\f' then return "'\\f'"  when '\n' then return "'\\n'"
  252.       when '\r' then return "'\\r'"  when '\t' then return "'\\t'"     
  253.       when '\v' then return "'\\v'"  when '\\' then return "'\\\\'"
  254.       when '\'' then return "'\\''"  
  255.       else s::=int.octal_str; return "'\\".append(s.tail(s.size-2),"'")
  256.       end end;
  257.  
  258.    upper:CHAR is 
  259.       -- An upper case version of self. 
  260.       -- Leaves non-alphabetic chars unchanged.
  261.       case self
  262.       when 'a' then return 'A'      when 'b' then return 'B'  
  263.       when 'c' then return 'C'      when 'd' then return 'D'  
  264.       when 'e' then return 'E'      when 'f' then return 'F'
  265.       when 'g' then return 'G'      when 'h' then return 'H'  
  266.       when 'i' then return 'I'      when 'j' then return 'J'  
  267.       when 'k' then return 'K'      when 'l' then return 'L'
  268.       when 'm' then return 'M'      when 'n' then return 'N'  
  269.       when 'o' then return 'O'      when 'p' then return 'P'  
  270.       when 'q' then return 'Q'      when 'r' then return 'R'
  271.       when 's' then return 'S'      when 't' then return 'T'  
  272.       when 'u' then return 'U'      when 'v' then return 'V'  
  273.       when 'w' then return 'W'      when 'x' then return 'X'
  274.       when 'y' then return 'Y'      when 'z' then return 'Z'
  275.       else return self end end;
  276.      
  277.    lower:CHAR is 
  278.       -- A lower case version of self.
  279.       -- Leaves non-alphabetic chars unchanged.
  280.       case self
  281.       when 'A' then return 'a'      when 'B' then return 'b'  
  282.       when 'C' then return 'c'      when 'D' then return 'd'  
  283.       when 'E' then return 'e'      when 'F' then return 'f'
  284.       when 'G' then return 'g'      when 'H' then return 'h'  
  285.       when 'I' then return 'i'      when 'J' then return 'j'  
  286.       when 'K' then return 'k'      when 'L' then return 'l'
  287.       when 'M' then return 'm'      when 'N' then return 'n'  
  288.       when 'O' then return 'o'      when 'P' then return 'p'  
  289.       when 'Q' then return 'q'      when 'R' then return 'r'
  290.       when 'S' then return 's'      when 'T' then return 't'  
  291.       when 'U' then return 'u'      when 'V' then return 'v'  
  292.       when 'W' then return 'w'      when 'X' then return 'x'
  293.       when 'Y' then return 'y'      when 'Z' then return 'z'
  294.       else return self end end;
  295.  
  296.    is_eq(c:SAME):BOOL is
  297.       -- True if self and `c' are equal.
  298.       builtin CHAR_IS_EQ; end;
  299.    
  300.    is_lt(c:SAME):BOOL is 
  301.       -- True if self is earlier than `c' in the ordering of
  302.       -- characters.
  303.       return int < c.int end;
  304.  
  305. end; -- immutable class CHAR
  306.  
  307. -------------------------------------------------------------------   
  308.